package com.wdress.actions;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.DynaActionForm;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.classic.Session;
import org.hibernate.transform.Transformers;

import com.wdress.base.BaseAction;
import com.wdress.model.Wdress;
import com.wdress.model.Wkeyword;

public class SearchAction extends BaseAction{

	@Override
	public String executeAction(DynaActionForm form, Session session,
			HttpServletRequest arg2, HttpServletResponse arg3,String lang) throws Exception {
		String keyword = form.getString("keyword");
		keyword=keyword.replaceAll("-", " ");
		arg2.setAttribute("keyword", keyword) ;
		Query query = session.createQuery("from Wdress where content like :param and lang=:lang");
		query.setParameter("param", "%"+keyword+"%");
		query.setParameter("lang", lang);
		List<Wdress> dresslist=query.setMaxResults(12).list();
		arg2.setAttribute("dresslist", dresslist);
		query = session.createQuery("from Wkeyword where types='categorie' and typeid=3 and lang=?");
		query.setString(0, lang);
		Wkeyword categorieObj= (Wkeyword) query.uniqueResult();
		arg2.setAttribute("categorieObj", categorieObj);
		SQLQuery sqlQuery = session.createSQLQuery("select b.keyword as color,b.url from wdress a,wkeyword b where a.color=b.keyword and a.lang=b.lang and a.dress_type=? and b.types ='color' and a.lang=? group by b.keyword having count(*)>0");
		sqlQuery.setString(0, categorieObj.getKeyword());
		sqlQuery.setString(1, lang);
		List colorlist = sqlQuery.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
		arg2.setAttribute("colorlist", colorlist) ;
		return "success";
	}

}
